import java.awt.Color;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import texture.Texture;


public class Menu {
	
	private static boolean run = true;
	private static int selection = 0;
	private static int selectionMap = 0;
	private static int selectionSettings = 0;
	private static int selectionCredits = 0;
	private static boolean showMainMenu = true;
	private static boolean showMaps = false;
	private static boolean showSettings = false;
	private static boolean showCredits = false;
	static int height, width;
	
	public static void showMenu(TrueTypeFont font){
//		BufferedImage img = loadImage("loading.jpg");
//		height = img.getHeight();
//		width = img.getWidth();
//		ByteBuffer bb = convertImageData(img);
//		System.out.println(height+"  "+width);

		SSG.set2DMode();
		while(run){
			renderMenu(font);
			processInput();
			Display.update();
			resetView();
		}
		SSG.set3DMode();
	}
	
	private static void renderMenu(TrueTypeFont font){

		  font.drawString(280, font.getHeight()*12, "Simple Shooting Game", 1.2f,1.2f);
		  if (showMaps){
			  if(selectionMap == 0)
				  font.drawString(400, font.getHeight()*9, "* Tutorial", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*9, "Tutorial", 1f,1f); 
			  
			  if(selectionMap == 1)
				  font.drawString(400, font.getHeight()*8, "* Map 1", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*8, "Map 1", 1f,1f);
		  }
		  if(showSettings){
			  String sel;
			  int endi = 0;
			  for(int i=0; i < 9; i++){	//show only first 9 resolutions, because of space limit
				  sel = SSG.dm[i].toString();
				  endi = sel.lastIndexOf('@');
				  if(selectionSettings == i)
					  font.drawString(400, font.getHeight()*(9-i), "*"+sel.substring(0, endi), 1f,1f);
				  else
					  font.drawString(400, font.getHeight()*(9-i), sel.substring(0, endi), 1f,1f);
			  }
		  }
		  if(showCredits){
			  font.drawString(400, font.getHeight()*9, "Jure Klancar", 1f,1f);
			  font.drawString(400, font.getHeight()*8, "Mitja Lapajne", 1f,1f);
			  font.drawString(400, font.getHeight()*7, "Robert Slak", 1f,1f);	  
		  }
		  if(showMainMenu){
			  if(selection == 0)
				  font.drawString(400, font.getHeight()*9, "* New Game", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*9, "New Game", 1f,1f);
			  
			  if(selection == 1)
				  font.drawString(400, font.getHeight()*8, "* Multiplayer", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*8, "Multiplayer", 1f,1f);
			  
			  if(selection == 2)
				  font.drawString(400, font.getHeight()*7, "* Settings", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*7, "Settings", 1f,1f);
			  
			  if(selection == 3)
				  font.drawString(400, font.getHeight()*6, "* Credits", 1f,1f);
			  else
				  font.drawString(400, font.getHeight()*6, "Credits", 1f,1f);
		  }
	}
	
	protected static void resetView()
	  {
	    // clear color and depth buffer
	    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	  }
	
	protected static void processInput(){
      
	    if (Display.isCloseRequested()) 
	    {
	      BaseWindow.closeGame();
	    } 
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
			if(showMaps){
				selectionMap = (selectionMap + 1) % 2;
			}
			if(showSettings){
				selectionSettings = (selectionSettings +1) % 9;
			}
			else{
				selection = (selection + 1) % 4;
			}
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)){
			if(showMaps){
				selectionMap = (selectionMap % 2)-1;
				if(selectionMap == -1)
					selectionMap = 1;
			}
			if(showSettings){
				selectionSettings = (selectionSettings % 9)-1;
				if(selectionSettings == -1)
					selectionSettings = 8;
			}
			else{
				selection = (selection % 4)-1;
				if(selection == -1)
					selection = 3;
			}
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_RETURN )){
			if(showMaps){
				if(selectionMap == 0){
					SSG.setMapName("Tutorial.bsp");
					run = false;
				}
				if(selectionMap == 1){
					SSG.setMapName("cht3.bsp");
					run = false;
				}
			}
			if(showSettings){
				if(selectionSettings == 0){
					try {
						Display.setDisplayMode(SSG.dm[0]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[0].getWidth();
					SSG.height = SSG.dm[0].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 1){
					try {
						Display.setDisplayMode(SSG.dm[1]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[1].getWidth();
					SSG.height = SSG.dm[1].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 2){
					try {
						Display.setDisplayMode(SSG.dm[2]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[2].getWidth();
					SSG.height = SSG.dm[2].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 3){
					try {
						Display.setDisplayMode(SSG.dm[3]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[3].getWidth();
					SSG.height = SSG.dm[3].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 4){
					try {
						Display.setDisplayMode(SSG.dm[4]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[4].getWidth();
					SSG.height = SSG.dm[4].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 5){
					try {
						Display.setDisplayMode(SSG.dm[5]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[5].getWidth();
					SSG.height = SSG.dm[5].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 6){
					try {
						Display.setDisplayMode(SSG.dm[6]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[6].getWidth();
					SSG.height = SSG.dm[6].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 7){
					try {
						Display.setDisplayMode(SSG.dm[7]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[7].getWidth();
					SSG.height = SSG.dm[7].getHeight();
					showSettings = false;
				}
				if(selectionSettings == 8){
					try {
						Display.setDisplayMode(SSG.dm[8]);
					} catch (LWJGLException e) {
						e.printStackTrace();
					}
					SSG.width  = SSG.dm[8].getWidth();
					SSG.height = SSG.dm[8].getHeight();
					showSettings = false;
				}
			}else 
				if(selection == 0){
					showMainMenu = false;
					showMaps = true;
				}
				if(selection == 1){
					showMainMenu = false;
					showMaps =true;
					SSG.multiplayer = true;
				}
				if(selection == 2){
					showMainMenu = false;
					showSettings =true;
				}
				if(selection == 3){
					showMainMenu = false;
					showCredits = true;
				}
			
			}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE )){
			if(showMaps){
				showMaps = false;
				showMainMenu = true;
				SSG.multiplayer = false;
			}
			if(showSettings){
				showSettings = false;
				showMainMenu = true;
			}
			if(showCredits){
				showCredits = false;
				showMainMenu = true;
			}
		}
		
	    if(Keyboard.isKeyDown(Keyboard.KEY_F3) && !SSG.fullscreen){
	    	SSG.fullscreen = true;
	    	try {
	    		Display.setFullscreen(true);
			} catch (LWJGLException e) {
				e.printStackTrace();
			}
	    }
	    if(Keyboard.isKeyDown(Keyboard.KEY_F4) && SSG.fullscreen){
	    	SSG.fullscreen = false;
	    	try {
	    		Display.setFullscreen(false);
			} catch (LWJGLException e) {
				e.printStackTrace();
			}
	    }
	    
		
		try {
			Thread.currentThread();
			Thread.sleep(90);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

    private static BufferedImage loadImage(String ref)
    { 
    	BufferedImage bufferedImage = null;
    	try{
    	InputStream is = new BufferedInputStream(new FileInputStream(ref));
	    bufferedImage = ImageIO.read(is);
    	}catch (Exception e){
    		System.out.println("Error loading menu.");
    	}
    	return bufferedImage;
    }

    private static ByteBuffer convertImageData(BufferedImage bufferedImage) { 
        ByteBuffer imageBuffer = null; 
        WritableRaster raster;
        BufferedImage texImage;
        
        
        /** The colour model for the GL image */
        ColorModel glColorModel;
        
        
        glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                new int[] {8,8,8,0},
                false,
                false,
                ComponentColorModel.OPAQUE,
                DataBuffer.TYPE_BYTE);
                

        int texWidth = 2;
        int texHeight = 2;
        
        // find the closest power of 2 for the width and height

        // of the produced texture

        while (texWidth < bufferedImage.getWidth()) {
            texWidth *= 2;
        }
        while (texHeight < bufferedImage.getHeight()) {
            texHeight *= 2;
        }
        

        
        // create a raster that can be used by OpenGL as a source

        // for a texture


        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
        texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
   
            
        // copy the source image into the produced image

        Graphics g = texImage.getGraphics();
        g.setColor(new Color(0f,0f,0f,0f));
        g.fillRect(0,0,texWidth,texHeight);
        g.drawImage(bufferedImage,0,0,null);
        
        // build a byte buffer from the temporary image 

        // that be used by OpenGL to produce a texture.

        byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData(); 

        imageBuffer = ByteBuffer.allocateDirect(data.length); 
        imageBuffer.order(ByteOrder.nativeOrder()); 
        imageBuffer.put(data, 0, data.length); 
        imageBuffer.flip();
        
        return imageBuffer; 
    } 
}

